//
//  SouceModel.m
//  testWebView
//
//  Created by ty804 on 18/05/2019.
//  Copyright © 2019 ty804. All rights reserved.
//




#import "SouceModel.h"
#import "Objc_Name.h"
#import "NSRegularExpression+SHY_GetValues.h"

static NSString *const MIX_SIGN_NAME_NEWLINE = @"\n";
static NSString *const MIX_SIGN_NAME_NSSTRING = @"NSString";
static NSString *const MIX_SIGN_NAME_NSINTEFER = @"NSInteger";
static NSString *const MIX_SIGN_NAME_BOOL = @"BOOL";
static NSString *const MIX_SIGN_NAME_P = @"@property";
static NSString *const MIX_SIGN_NAME_I = @"@interface";
static NSString *const MIX_SIGN_NAME_E= @"@end";
static NSString *const MIX_SIGN_NAME_L = @";";
static NSString *const MIX_SIGN_NAME_R = @"return";
static NSString *const MIX_SIGN_NAME_IM = @"@implementation";

@interface SouceModel ()

@end


@implementation SouceModel
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.arrMixCode_Type=[NSMutableArray array];
    }
    return self;
}
-(void)setArrSouce:(NSArray *)arrSouce{
    _arrSouce=arrSouce;
    NSString *str=[self.arrSouce componentsJoinedByString:MIX_SIGN_NAME_NEWLINE];
    self.classSouce=str;
}
-(void)setIndex:(NSInteger)index{
    _index=index;
    self.arrPrameterName=[NSMutableArray arrayWithCapacity:index];
    for (int i=0; i<_index; i++) {
        [self.arrPrameterName addObject:[Objc_Name arc4random_prameter_name]];
    }
    self.arrMixCode=[NSMutableArray arrayWithCapacity:index];
    NSArray *arr=@[MIX_SIGN_NAME_NSSTRING,MIX_SIGN_NAME_NSINTEFER,MIX_SIGN_NAME_BOOL];
    for (int i=0; i<_index; i++) {
        NSString *type=arr[arc4random_uniform(arr.count)];
        NSString *pra_name=self.arrPrameterName[i];
        if ([type isEqualToString:MIX_SIGN_NAME_NSSTRING]) {
            NSString *str=[NSString stringWithFormat:@"@property (nonatomic,strong)NSString *%@;",pra_name];
            [self.arrMixCode addObject:str];
            [self.arrMixCode_Type addObject:[NSNumber numberWithInteger:MixCode_TypeNSString]];
            
        }else if ([type isEqualToString:MIX_SIGN_NAME_NSINTEFER]){
            NSString *str=[NSString stringWithFormat:@"@property (nonatomic,assign)NSInteger %@;",pra_name];
           [self.arrMixCode addObject:str];
           [self.arrMixCode_Type addObject:[NSNumber numberWithInteger:MixCode_TypeNSInteger]];
            
        }else{
            NSString *str=[NSString stringWithFormat:@"@property (nonatomic,assign)BOOL %@;",pra_name];
            [self.arrMixCode addObject:str];
            [self.arrMixCode_Type addObject:[NSNumber numberWithInteger:MixCode_TypeBOOL]];
        }
    }
    
}
-(void)insertStatementCode;{

    NSArray<NSString *> *arrPart=[NSRegularExpression arrayOfCheckStringWithRegularExpression:@"(?<=@interface).*?(?=@end)" checkString:self.classSouce];
    if (!arrPart||arrPart.count<=0)return;
    NSString *str=arrPart.firstObject;
    if ([str localizedStandardContainsString:@"+"])return;
    NSMutableArray *arrInfo=[NSMutableArray arrayWithArray:self.arrSouce];
    if (![str localizedStandardContainsString:MIX_SIGN_NAME_P]) {
        [self.arrSouce enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj localizedStandardContainsString:MIX_SIGN_NAME_I]) {
                *stop=YES;
                for (int i=0; i<self.arrMixCode.count; i++) {
                    [arrInfo insertObject:self.arrMixCode[i] atIndex:idx+1];
                }
                self.arrSouce=[NSArray arrayWithArray:arrInfo];
                return ;
            }
        }];
        
    }else{
        [self.arrSouce enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj localizedStandardContainsString:MIX_SIGN_NAME_P]) {
                *stop=YES;
                for (int i=0; i<self.arrMixCode.count; i++) {
                    [arrInfo insertObject:self.arrMixCode[i] atIndex:idx+1];
                }
                self.arrSouce=[NSArray arrayWithArray:arrInfo];
                return ;
            }
        }];
        
    }
}
-(void)insertFunctionCode;{
    NSMutableArray *arrInfo=[NSMutableArray arrayWithArray:self.arrSouce];
    NSMutableArray *arrNum_index=[NSMutableArray array];
    if ([self isCloudWord]) {
        [self.arrSouce enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([self idexPathWithWord]>0&&idx>[self idexPathWithWord]&&idx>[self idexPathWithWordClassType]) {
                if (![obj localizedStandardContainsString:MIX_SIGN_NAME_E]) {
                    if ([obj localizedStandardContainsString:MIX_SIGN_NAME_L]) {
                        [arrNum_index addObject:[NSString stringWithFormat:@"%lu",(unsigned long)idx]];
                    }
                }
            }
        }];

        NSArray *arr=[[NSArray alloc]initWithArray:self.arrMixCode];
        NSArray *arr1=[[NSArray alloc]initWithArray:self.arrMixCode_Type];
        NSArray *arr2=[[NSArray alloc]initWithArray:self.arrPrameterName];
        for (int i=0; i<self.ratio-1; i++) {
            [self.arrMixCode addObjectsFromArray:arr];
            [self.arrMixCode_Type addObjectsFromArray:arr1];
            [self.arrPrameterName addObjectsFromArray:arr2];
        }
        __block NSUInteger idx_Mix_start=0;
        NSMutableArray *arr_mix=[self mix_code:self.arrMixCode];
        [arr_mix enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSInteger index=arc4random_uniform(arrNum_index.count);
            NSString *strIndex=arrNum_index[index];
            NSString *str_index_code=self.arrSouce[strIndex.integerValue];
            if (![str_index_code localizedStandardContainsString:MIX_SIGN_NAME_R]) {
                NSInteger index_loc=[arrInfo indexOfObject:str_index_code];
                NSUInteger space=[self codeSpace:str_index_code];
                NSString *space_code=[self addSpace:obj index:space];
                [arrInfo insertObject:space_code atIndex:index_loc+1];
                idx_Mix_start++;
            }
        }];
        
        self.arrSouce=arrInfo;
    }
}
-(void)insertClassCode:(NSArray<ClassModel *> *)arrClassModel{
    NSMutableArray *arrInfo=[NSMutableArray arrayWithArray:self.arrSouce];
    NSMutableArray *arrNum_index=[NSMutableArray array];
    [arrClassModel enumerateObjectsUsingBlock:^(ClassModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.classFunctionName isEqualToString:@""]) {
            if ([self isCloudWord]) {
                [self.arrSouce enumerateObjectsUsingBlock:^(NSString*  _Nonnull objSouce, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([self idexPathWithWord]>0&&idx>[self idexPathWithWord]&&idx>[self idexPathWithWordClassType]) {
                        if (![objSouce localizedStandardContainsString:MIX_SIGN_NAME_E]) {
                            if ([objSouce localizedStandardContainsString:MIX_SIGN_NAME_L]) {
                                [arrNum_index addObject:[NSString stringWithFormat:@"%lu",(unsigned long)idx]];
                            }
                        }
                    }
                }];
               
                NSInteger count=arc4random() % [arrNum_index count];
                NSString *strIndex=arrNum_index[count];
                NSString *str_index_code=self.arrSouce[strIndex.integerValue];
                if (![str_index_code localizedStandardContainsString:MIX_SIGN_NAME_R]) {
                    NSInteger index_loc=[arrInfo indexOfObject:str_index_code];
                    NSUInteger space=[self codeSpace:str_index_code];
                    NSString *outPut=[obj codeByOutPut];
                    NSString *space_code=[self addSpace:outPut index:space];
                    [arrInfo insertObject:space_code atIndex:index_loc+1];
                    NSLog(@"插入完毕");
                    self.arrSouce=arrInfo;
                    
                }
            }
           
            
            
        }else{
            NSArray<NSString *> *arrPart=[NSRegularExpression arrayOfCheckStringWithRegularExpression:@"(?<=@interface).*?(?=@end)" checkString:self.classSouce];
            if (!arrPart||arrPart.count<=0)return;
            NSString *str=arrPart.firstObject;
            if ([str localizedStandardContainsString:@"+"])return;
            NSMutableArray *arrInfo=[NSMutableArray arrayWithArray:self.arrSouce];
            if (![str localizedStandardContainsString:MIX_SIGN_NAME_P]) {
                [self.arrSouce enumerateObjectsUsingBlock:^(NSString *  _Nonnull strObj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([strObj localizedStandardContainsString:MIX_SIGN_NAME_I]) {
                        *stop=YES;
                        [obj codeByOutPut];
                        [arrInfo insertObject:obj.classPropertyName atIndex:idx+1];
                        self.arrSouce=[NSArray arrayWithArray:arrInfo];
                        return ;
                    }
                }];
                
            }else{
                [self.arrSouce enumerateObjectsUsingBlock:^(NSString *  _Nonnull strObj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([strObj localizedStandardContainsString:MIX_SIGN_NAME_P]) {
                        *stop=YES;
                        [obj codeByOutPut];
                       [arrInfo insertObject:obj.classPropertyName atIndex:idx+1];
                        
                        self.arrSouce=[NSArray arrayWithArray:arrInfo];
                        return ;
                    }
                }];
                
            }
            
            if ([self isCloudWord]) {
                [self.arrSouce enumerateObjectsUsingBlock:^(NSString*  _Nonnull objSouce, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([self idexPathWithWord]>0&&idx>[self idexPathWithWord]&&idx>[self idexPathWithWordClassType]) {
                        if (![objSouce localizedStandardContainsString:MIX_SIGN_NAME_E]) {
                            if ([objSouce localizedStandardContainsString:MIX_SIGN_NAME_L]) {
                                [arrNum_index addObject:[NSString stringWithFormat:@"%lu",(unsigned long)idx]];
                            }
                        }
                    }
                }];
                
                NSInteger count=arc4random() % [arrNum_index count];
                NSString *strIndex=arrNum_index[count];
                NSString *str_index_code=self.arrSouce[strIndex.integerValue];
                if (![str_index_code localizedStandardContainsString:MIX_SIGN_NAME_R]) {
                    NSInteger index_loc=[arrInfo indexOfObject:str_index_code];
                    NSUInteger space=[self codeSpace:str_index_code];
                    NSString *space_code=[self addSpace:obj.classIntPutSouce index:space];
                    [arrInfo insertObject:space_code atIndex:index_loc+1];
                    NSLog(@"插入完毕");
                    self.arrSouce=arrInfo;
                    
                }
            }
        }
    }];
    
}

-(BOOL)isCloudWord{
    BOOL isYes =NO;
    for (NSString *obj in self.arrSouce) {
        if ([obj localizedStandardContainsString:@"AppDelegate"]) {
            return NO;
        }
        if ([obj localizedStandardContainsString:@"Reachability"]) {
            return NO;
        }
        if ([obj localizedStandardContainsString:MIX_SIGN_NAME_IM]) {

            isYes=YES;
        }
    }
    return isYes;
}
//获取@implementation 第几行
-(NSUInteger)idexPathWithWord{
    for (NSString *obj in self.arrSouce) {
        if ([obj localizedStandardContainsString:MIX_SIGN_NAME_IM]) {
            return [self.arrSouce indexOfObject:obj];
        }
    }
    return 0;
}
//当代码块里面包含+方法，获取类方法最后一行index
-(NSUInteger)idexPathWithWordClassType{
    BOOL isY=NO;
    for (NSString *obj in self.arrSouce) {
        if ([obj hasPrefix:@"+"]) {
            isY=YES;
        }
    }
    for (NSString *obj in self.arrSouce) {
        if (isY) {
            if ([obj isEqualToString:@"}"]) {
                
                return [self.arrSouce indexOfObject:obj];
            }
            
        }
    }
    return 0;
}
-(NSMutableArray *)mix_code:(NSMutableArray *)arr_mix{
    NSMutableArray *arr=[NSMutableArray array];
    [arr_mix enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSNumber *type=self.arrMixCode_Type[idx];
        switch (type.integerValue) {
            case 0:
              
                [arr addObject:[Objc_Name FUNCTION_MIX_NSSTRING:self.arrPrameterName[idx]]];
                break;
            case 1:
                
                [arr addObject:[Objc_Name FUNCTION_MIX_BOOL:self.arrPrameterName[idx]]];
                break;
            
            case 2:
                
                [arr addObject:[Objc_Name FUNCTION_MIX_INT:self.arrPrameterName[idx]]];
                break;
   
            default:
                break;
        }
    }];
    
    return arr;
    
}
-(NSUInteger)codeSpace:(NSString *)code{
    NSUInteger before=code.length;
    NSString *str=[code stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    return before-str.length>0?before-str.length:0;
}
-(NSString *)addSpace:(NSString *)code index:(NSUInteger)index{
    NSString *last=code;
    for (int i=0; i<index; i++) {
        last=[NSString stringWithFormat:@"%@%@",@" ",last];
    }
    return last;
}
@end
